package com.senseu.pages;

import com.senseu.base.BasePage;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class LoginPage extends BasePage {

    public LoginPage(AppiumDriver driver) {
        super(driver);
    }

    @FindBy(id = "com.senseu.baby:id/bt_login")
    private WebElement btnLogin;

    @FindBy(id = "com.senseu.baby:id/login_username")
    private WebElement etUserName;

    @FindBy(id = "com.senseu.baby:id/login_password")
    private WebElement etPassword;

    public void login(String username, String password) {
        etUserName.clear();
        etUserName.sendKeys(username);
        etPassword.clear();
        etPassword.sendKeys(password);
        btnLogin.click();
    }

    private final By toast = By.xpath("//android.widget.Toast");

    public BasePage isLoginSuccess() {
        boolean present = isInPage(10,"NewHomeActivity","ChooseDevice2BindByRemoteConfigActivity");
        if (present) {
            // 登录成功
            if (getPage().contains("NewHomeActivity")) {
                return new HomePage(driver);
            } else {
                return new SelectDevice2BindParentPage(driver);
            }
        } else  {
            // 登录失败
            return null;
        }
    }

    public boolean isLoginFailed() {
        WebElement element = waitToast(toast, 15);
        if (element != null) {
            logger.info("登录失败 吐司内容：{}", element.getText());
        }
        return element != null;
    }

}
